#include<avr/io.h>
#include<avr/eeprom.h>
#include"variable.h"


void UDCHPA_to_SPC(void)
{
	uint16 UDC_UP_Mid_frq;
	uint16 UDC_DOWN_Mid_frq;
	uint16 SPC_UP_Mid_frq;
	uint16 SPC_DOWN_Mid_frq;
	


	if( UDC_CRC_Check8(&UDC_RX_BUFF[0] , 14U ) == 1U)
	{
			
		if((PRA_CTL & 0x01U) == 0x01U )
		{
				UDC_UP_Mid_frq = (UDC_RX_BUFF[5]*256U);
				UDC_DOWN_Mid_frq = (UDC_RX_BUFF[10]*256U);

			     UDC_RX_UP_FREQUENCY = ( UDC_UP_Mid_frq | UDC_RX_BUFF[4] );
				UDC_RX_DOWN_FREQUENCY = ( UDC_DOWN_Mid_frq | UDC_RX_BUFF[9] );

				
				if(UDC_RX_BUFF[6] == 0x15U )
				{
					switch(UDC_RX_UP_FREQUENCY)
					{

				    case 0x0735U:
							SPC_TX_BUFF[6] = 0x01U;
						break;
			
						case 0x0795U:
							SPC_TX_BUFF[6] = 0x02U;
						break;
			
						case 0x0855U:
							SPC_TX_BUFF[6] = 0x03U;
						break;

						case 0x0915U:
							SPC_TX_BUFF[6] = 0x04U;
						break;

						case 0x0975U:
							SPC_TX_BUFF[6] = 0x05U;
						break;

						case 0x1035U:
							SPC_TX_BUFF[6] = 0x06U;
						break;

						case 0x1095U:
							SPC_TX_BUFF[6] = 0x07U;
						break;

						case 0x1155U:
							SPC_TX_BUFF[6] = 0x08U;
						break;

						case 0x1215U:
							SPC_TX_BUFF[6] = 0x09U;
						break;

						case 0x1275U:
							SPC_TX_BUFF[6] = 0x0AU;
						break;

						case 0x1335U:
							SPC_TX_BUFF[6] = 0x0BU;
						break;

						case 0x1395U:
							SPC_TX_BUFF[6] = 0x0CU;
						break;

						case 0x1455U:
							SPC_TX_BUFF[6] = 0x0DU;
						break;

                        default:
                        break;

					}
				
				}


					if( UDC_RX_BUFF[11] == 0x14U )
					{ 

						switch(UDC_RX_DOWN_FREQUENCY)
						{

						case 0x9055U:
								SPC_TX_BUFF[7] = 0x09U;
							break;
				
							case 0x9115U:
								SPC_TX_BUFF[7] = 0x0AU;
							break;
				
							case 0x9175U:
								SPC_TX_BUFF[7] = 0x0BU;
							break;

							case 0x9235U:
								SPC_TX_BUFF[7] = 0x0CU;
							break;

							case 0x9295U:
								SPC_TX_BUFF[7] = 0x0DU;
							break;

							case 0x9355U:
								SPC_TX_BUFF[7] = 0x01U;
							break;

							case 0x9415U:
								SPC_TX_BUFF[7] = 0x02U;
							break;

							case 0x9475U:
								SPC_TX_BUFF[7] = 0x03U;
							break;

							case 0x9535U:
								SPC_TX_BUFF[7] = 0x04U;
							break;

							case 0x9595U:
								SPC_TX_BUFF[7] = 0x05U;
							break;

							case 0x9655U:
								SPC_TX_BUFF[7] = 0x06U;
							break;

							case 0x9715U:
								SPC_TX_BUFF[7] = 0x07U;
							break;

							case 0x9775U:
								SPC_TX_BUFF[7] = 0x08U;
							break;

                            default:
                            break;
	
						}
			
					}

				}


				SPC_UP_Mid_frq = (UDC_RX_BUFF[5]*256U);
					
				 SPC_TX_UP_Fre_Step_hex = ( SPC_UP_Mid_frq + UDC_RX_BUFF[4] );
				 up_gap = SPC_TX_UP_Fre_Step_hex - first_up_ch;
			   	 
				
				if(up_gap < 768U )
				{
					SPC_TX_BUFF[18] = (uint8)((up_gap/16U) - (((up_gap/256U) * 96U )/16U)); 
				}
                else
                {                   
                   	SPC_TX_BUFF[18] = (uint8)((up_gap/16U) - ((((up_gap+2560U)/256U) * 96U )/16U)); 
                }


				SPC_DOWN_Mid_frq = (UDC_RX_BUFF[10]*256U);
				
				SPC_TX_DOWN_Fre_Step_hex = (SPC_DOWN_Mid_frq + UDC_RX_BUFF[9]);

				if(SPC_TX_DOWN_Fre_Step_hex >= first_down_ch )
				{
					down_gap = SPC_TX_DOWN_Fre_Step_hex - first_down_ch;
				}
				else
				{
					down_gap = SPC_TX_DOWN_Fre_Step_hex - 0x8D15U;
				}

				

				
				if(((SPC_TX_DOWN_Fre_Step_hex & 0x00F0U) == 0x0000U) || ((SPC_TX_DOWN_Fre_Step_hex & 0x00F0U) == 0x0010U) )
				{
					 SPC_TX_BUFF[19] = (uint8)((down_gap/16U) - ((((down_gap+32U)/256U) * 96U )/16U)); 
                      
				}
				else
				{

					 SPC_TX_BUFF[19] = (uint8)((down_gap/16U) - (((down_gap/256U) * 96U )/16U)); 
				}
				
	
				
				
		}

}
